package com.pyj.topline.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.pyj.topline.R;
import com.pyj.topline.activity.*;
import com.pyj.topline.receiver.UpdateUserInfoReceiver;
import com.pyj.topline.utils.DBUtils;
import com.pyj.topline.utils.UtilsHelper;
import de.hdodenhof.circleimageview.CircleImageView;

public class MeFragment extends Fragment implements View.OnClickListener {
    private LinearLayout ll_calendar, ll_constellation, ll_scraw, ll_map;
    private RelativeLayout rl_collection, rl_setting;
    private CircleImageView iv_avatar;
    private View view;
    private UpdateUserInfoReceiver updateUserInfoReceiver;
    private IntentFilter filter;
    private boolean isLogin = false;
    private CollapsingToolbarLayout collapsingToolbarLayout;

    public MeFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        ll_calendar = view.findViewById(R.id.ll_calendar);
        ll_constellation = view.findViewById(R.id.ll_constellation);
        ll_scraw = view.findViewById(R.id.ll_scraw);
        ll_map = view.findViewById(R.id.ll_map);
        rl_collection = view.findViewById(R.id.rl_collection);
        rl_setting = view.findViewById(R.id.rl_setting);
        iv_avatar = view.findViewById(R.id.iv_avatar);
        collapsingToolbarLayout = view.findViewById(R.id.collapsing_tool_bar);
        collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ToolbarTitle);
        isLogin = UtilsHelper.readLoginStatus(getActivity());
        setLoginParams(isLogin);
        setListener();
        receiver();
    }

    private void receiver() {
        updateUserInfoReceiver = new UpdateUserInfoReceiver(
                new UpdateUserInfoReceiver.BaseOnReceiveMsgListener() {
                    @Override
                    public void onReceiveMsg(Context context, Intent intent) {
                        String action = intent.getAction();
                        if (UpdateUserInfoReceiver.ACTION.UPDATE_USERINFO.equals(action)) {
                            String type = intent.getStringExtra(UpdateUserInfoReceiver.INTENT_TYPE.TYPE_NAME);
                            if (UpdateUserInfoReceiver.INTENT_TYPE.UPDATE_HEAD.equals(type)) {
                                String head = intent.getStringExtra("head");
                                Bitmap bt = BitmapFactory.decodeFile(head);
                                if (bt != null) {
                                    Drawable drawable = new BitmapDrawable(bt);
                                    iv_avatar.setImageDrawable(drawable);
                                } else {
                                    iv_avatar.setImageResource(R.drawable.avatar);
                                }
                            }
                        }
                    }
                });
        filter = new IntentFilter(UpdateUserInfoReceiver.ACTION.UPDATE_USERINFO);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            getActivity().registerReceiver(updateUserInfoReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
        } else {
            getActivity().registerReceiver(updateUserInfoReceiver, filter);
        }
    }

    private void setListener() {
        ll_calendar.setOnClickListener(this);
        ll_constellation.setOnClickListener(this);
        ll_scraw.setOnClickListener(this);
        ll_map.setOnClickListener(this);
        rl_collection.setOnClickListener(this);
        rl_setting.setOnClickListener(this);
        iv_avatar.setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (updateUserInfoReceiver != null) {
            getActivity().unregisterReceiver(updateUserInfoReceiver);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ll_calendar) {
            startActivity(new Intent(getActivity(), CalendarActivity.class));
        } else if (view.getId() == R.id.ll_constellation) {
            startActivity(new Intent(getActivity(), ConstellationActivity.class));
        } else if (view.getId() == R.id.ll_scraw) {
            startActivity(new Intent(getActivity(), ScrawActivity.class));
        } else if (view.getId() == R.id.ll_map) {
            startActivity(new Intent(getActivity(), MapActivity.class));
        } else if (view.getId() == R.id.rl_collection) {
            if (isLogin) {
                startActivity(new Intent(getActivity(), CollectionActivity.class));
            } else {
                Toast.makeText(getActivity(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            }
        } else if (view.getId() == R.id.rl_setting) {
            if (isLogin) {
                Intent settingIntent = new Intent(getActivity(), SettingActivity.class);
                startActivityForResult(settingIntent, 1);
            } else {
                Toast.makeText(getActivity(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            }
        } else if (view.getId() == R.id.iv_avatar) {
            if (isLogin) {
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
            } else {
                Intent login = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(login, 1);
            }
        }
    }

    public void setLoginParams(boolean isLogin) {
        if (isLogin) {
            String userName = UtilsHelper.readLoginUserName(getActivity());
            collapsingToolbarLayout.setTitle(userName);
            String head = DBUtils.getInstance(getActivity()).getUserHead(userName);
            Bitmap bt = BitmapFactory.decodeFile(head);
            if (bt != null) {
                Drawable drawable = new BitmapDrawable(bt);
                iv_avatar.setImageDrawable(drawable);
            } else {
                iv_avatar.setImageResource(R.drawable.avatar);
            }
        } else {
            iv_avatar.setImageResource(R.drawable.avatar);
            collapsingToolbarLayout.setTitle("点击登录");
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            boolean isLogin = data.getBooleanExtra("isLogin", false);
            setLoginParams(isLogin);
            this.isLogin = isLogin;
        }
    }
}